package pattern;

/**
 * Created with IntelliJ IDEA.
 * Description:单例模式-懒汉模式
 * User: bjc
 * Date: 2025-08-21
 * Time: 21:26
 */
class SingletonLazy{
    //创建实例但是不初始化
    private static SingletonLazy instance = null;
    private SingletonLazy(){}
    //第一次使用的时候才创建实例，而不是类加载时创建
    public static SingletonLazy getInstance(){
        if(instance == null){
            instance = new SingletonLazy();
        }
        return instance;
    }

}



public class Test2 {
    public static void main(String[] args) {
        SingletonLazy s = SingletonLazy.getInstance();
        SingletonLazy s1 = SingletonLazy.getInstance();
        System.out.println(s == s1);// 获取到的实例对象是同一个，说明类的实例只有一个
    }
}
